<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>fantastic</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        body {
            overflow: hidden;
        }
    </style>
    <link href="/component/home/css/font.css" rel="stylesheet">
    <link rel="stylesheet" href="/component/home/css/style.css">
    <link rel="stylesheet" href="/component/home/css/style-search.css" media="screen" type="text/css" />
    <script src="http://www.jq22.com/jquery/jquery-1.10.2.js"></script>
    <script src="/js/axios.min.js"></script>

    <script src="/component/tips/js/coco-message.js"></script>

    <script>
        let isRequest = false;

        let closeMsg ;
        //定时清理不允许访问的条件
        function clearSearchState(){
            setInterval(function (){
                isRequest = false ;
            },300000)
        }

        //为excel生成以时间作为文件的名字
        function getFileName(){
            var now = new Date();
            var file_name = now.getFullYear() + '-' +
                (now.getMonth() < 9 ? '0' : '') + (now.getMonth() + 1) + '-' +
                (now.getDate() < 10 ? '0' : '') + now.getDate() + ' ' +
                (now.getHours() < 10 ? '0' : '') + now.getHours() + ':' +
                (now.getMinutes() < 10 ? '0' : '') + now.getMinutes() + ':' +
                (now.getSeconds() < 10 ? '0' : '') + now.getSeconds();
            return file_name ;
        }

        function handleResponse(response) {
            const url = window.URL.createObjectURL(new Blob([response.data]));
            const link = document.createElement('a');
            link.href = url;
            link.setAttribute('download', getFileName()+'.xlsx');
            document.body.appendChild(link);
            link.click();
        }


        function search(){

            if (isRequest != false){
                cocoMessage.warning("当前请求还未完成", 2000);
                return ;
            }

            isRequest = true ;

            let token = $('#input').val();
            //请求后开始显示正在加载提示
            closeMsg = cocoMessage.loading(true)

            const instance = axios.create({
                baseURL: 'http://47.114.73.72:5588',
                // baseURL: 'http://localhost:5588',
                timeout: 300000,
                headers: {'X-Custom-Header': 'foobar','token': token}
            });

            instance.get('/api/student/exportExcel', {
                // 告诉 axios 以二进制流接收响应数据
                responseType: 'blob'
            })
                .then(response => {
                    //处理下载好的数据整理成Excel
                    handleResponse(response);
                    setTimeout(function () {
                        closeMsg();
                    }, 0);
                    cocoMessage.info(2000, "下载成功！", function () {
                        console.log("下载成功");
                        isRequest = false ;
                    });
                })
                .catch(error => {
                    cocoMessage.error("下载失败！", 2000);
                    console.error('下载 Excel 失败', error);
                    clearSearchState();
                });
        }

        $(document).on('keydown',function (event){
            if (event.keyCode == 13){

                if (isRequest == false){
                    search();
                }else {
                    cocoMessage.warning("当前请求还未完成", 2000);
                }
            }
        })
    </script>
</head>

<body>
<canvas id="canvas" width="1280" height="1024"> 您的浏览器不支持canvas标签，请您更换浏览器 </canvas>
<script src="/component/home/js/word.js"></script>
<p id="offscreen-text" class="offscreen-text"></p>
<p id="text" class="text"></p>
<!--<svg id="svg" > </svg>-->

<div id="d" class="webdesigntuts-workshop"> <span>
  <input class="input" id="input" type="search" placeholder="请输入您要搜索的内容" autofocus="autofocus"/>
  <button  onclick="search();">获取</button>
  </span> </div>

<script  src='/component/home/js/index.js'></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/2.0.1/TweenMax.min.js"></script>
</body>
</html>
